package com.settlement.system.service.impl;

import com.settlement.system.common.util.StringUtils;
import com.settlement.system.model.entity.PddOrder;
import com.settlement.system.mapper.PddOrderMapper;
import com.settlement.system.model.entity.SysStore;
import com.settlement.system.service.PddOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.settlement.system.service.SysStoreService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.model.query.PddOrderQuery;
import com.settlement.system.model.form.PddOrderForm;
import cn.hutool.core.bean.BeanUtil;
import com.settlement.system.model.vo.PddOrderImportVo;

/**
 * <p>
 * 拼多多订单 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-04-13
 */
@Service
@RequiredArgsConstructor
public class PddOrderServiceImpl extends ServiceImpl<PddOrderMapper, PddOrder> implements PddOrderService {

    private final SysStoreService sysStoreService;

    @Override
    public Page<PddOrder> getPage(PddOrderQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        String tableName = "pdd_order_%s".formatted(queryParams.getBillMonth());
        queryParams.setTableName(tableName);
        if (!StringUtils.hasText(this.baseMapper.existTable(tableName))) {
            return new Page<>(pageNum, pageSize);
        }
        Page<PddOrder> page = new Page<>(pageNum, pageSize);
        this.baseMapper.getPage(page, queryParams);
        SysStore sysStore;
        for (PddOrder record : page.getRecords()) {
            sysStore = sysStoreService.getById(record.getStoreId());
            if (sysStore != null) {
                record.setStoreName(sysStore.getBillSystemStoreName());
            }
        }
        return page;
    }

     @Override
     public boolean savePddOrder(PddOrderForm pddOrderForm) {
         PddOrder pddOrder = new PddOrder();
         BeanUtil.copyProperties(pddOrderForm, pddOrder);
         pddOrder.setCreateTime(null);
         return this.saveOrUpdate(pddOrder);
     }

     @Override
     public PddOrderForm getPddOrderFormData(Long id) {
         PddOrder pddOrder = this.getById(id);
         PddOrderForm pddOrderForm = new PddOrderForm();
         BeanUtil.copyProperties(pddOrder, pddOrderForm);
         return pddOrderForm;
     }

     @Override
     public boolean deletePddOrder(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
     }

    @Override
    public void importData(List<PddOrderImportVo> voList)  {
        List<PddOrder> pddOrderList = new ArrayList<>();
        for (PddOrderImportVo importVo : voList) {
            PddOrder pddOrder = new PddOrder();
            BeanUtil.copyProperties(importVo, pddOrder);
            pddOrderList.add(pddOrder);
        }
        this.saveOrUpdateBatch(pddOrderList);
    }

    @Override
    public List<PddOrderImportVo> findByIds(String idsStr) {
        if (StrUtil.isBlank(idsStr)) {
            return new ArrayList<PddOrderImportVo>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<PddOrder> pddOrderList = this.getBaseMapper().selectBatchIds(ids);
        List<PddOrderImportVo> voList = new ArrayList<>();
        for (PddOrder pddOrder : pddOrderList) {
            PddOrderImportVo vo = new PddOrderImportVo();
            BeanUtil.copyProperties(pddOrder, vo);
            voList.add(vo);
        }
        return voList;
    }
}
